//
//  MyOrderListTableViewCell.m
//  Findif
//
//  Created by Mac on 2018/11/1.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "MyOrderListTableViewCell.h"
#import "XHStarRateView.h"

@interface MyOrderListTableViewCell ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UILabel * stateLabel;
@property (nonatomic, weak)UILabel * goodsLabel;
@property (nonatomic, strong)UILabel * orderNumLabel;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, weak)UIImageView * goodsImageView;
@property (nonatomic, weak)UILabel * countLabel;
@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UILabel * commitLabel;
@property (nonatomic, weak)UILabel * dateLabel;
@property (nonatomic, weak)UILabel * priceLabel;
@property (nonatomic, weak)UILabel * orignLabel;
@property (nonatomic, weak)UILabel * totalMoneyLabel;
@property (nonatomic, weak)UIView * smallView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UILabel * orderDateLabel;
@property (nonatomic, weak)UIButton * cancleButton;
@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic, weak)UIImageView * discountsImageView;
@property (nonatomic, weak)UILabel * discountsLabel;
@property (nonatomic, strong)UILabel * totalLabel;
@property (nonatomic, weak)UILabel * moneyLabel;

@end

@implementation MyOrderListTableViewCell


- (void)setModel:(ShopOrderModel *)model{
    
    _model = model;
    
    self.orderNumLabel.text = [NSString stringWithFormat:@"订单编码:%@",model.goods_order_no];
    NSArray *array = [model.merchants_goods_img componentsSeparatedByString:@","];
    
    NSString * goodsStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,array.firstObject];
    NSURL * goodsUrl = [NSURL URLWithString:goodsStr];
    [self.goodsImageView sd_setImageWithURL:goodsUrl];
    self.goodsLabel.text = model.merchants_goods_name;
//    self.dateLabel.text = model.merchants_goods_available_time;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",model.merchatns_goods_platform_discount_price];
//    self.orignLabel.text = [NSString stringWithFormat:@"原价￥%@",model.merchants_goods_price];
    NSString * orString = [NSString stringWithFormat:@"￥%@",model.merchatns_goods_platform_price];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:orString attributes:attribtDic];
    
    self.orignLabel.attributedText = attribtStr;
    
    self.orderDateLabel.text = [NSString stringWithFormat:@"订单时间:%@",model.goods_order_inputtime];
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",model.goods_order_money];
    self.countLabel.text = [NSString stringWithFormat:@"*%@",model.goods_order_buy_number];
    int status = [model.goods_order_status intValue];
    if (status == 0){
        
        self.stateLabel.text = @"待付款";
        self.discountsImageView.hidden = NO;
        self.discountsLabel.hidden = NO;
        [self.cancleButton setTitle:@"取消订单" forState:UIControlStateNormal];
        [self.sureButton setTitle:@"立即付款" forState:UIControlStateNormal];
    }else if (status == 1){
        
        self.stateLabel.text = @"待使用";
        self.discountsImageView.hidden = YES;
        self.discountsLabel.hidden = YES;
        [self.cancleButton setTitle:@"取消订单" forState:UIControlStateNormal];
        [self.sureButton setTitle:@"立即使用" forState:UIControlStateNormal];
    }else if (status == 3){
        
        self.stateLabel.text = @"待评价";
        self.discountsImageView.hidden = YES;
        self.discountsLabel.hidden = YES;
        [self.cancleButton setTitle:@"再来一单" forState:UIControlStateNormal];
        [self.sureButton setTitle:@"立即评价" forState:UIControlStateNormal];
    }else if (status == 2){
        
        self.stateLabel.text = @"已取消";
        self.discountsImageView.hidden = YES;
        self.discountsLabel.hidden = YES;
        [self.cancleButton setTitle:@"删除订单" forState:UIControlStateNormal];
        [self.sureButton setTitle:@"再来一单" forState:UIControlStateNormal];
    }else if (status == 4){
        
        self.stateLabel.text = @"已失效";
        self.discountsImageView.hidden = YES;
        self.discountsLabel.hidden = YES;
        [self.cancleButton setTitle:@"删除订单" forState:UIControlStateNormal];
        [self.sureButton setTitle:@"再来一单" forState:UIControlStateNormal];
    }else if (status == 5){
        
        self.stateLabel.text = @"已评价";
        self.discountsImageView.hidden = YES;
        self.discountsLabel.hidden = YES;
        [self.cancleButton setTitle:@"删除订单" forState:UIControlStateNormal];
        [self.sureButton setTitle:@"再来一单" forState:UIControlStateNormal];
    }
    
    int merchants_goods_comments_star_level = [model.merchants_goods_comments_star_level intValue];
    if (merchants_goods_comments_star_level == 0) {
        
        self.starView.width = 0;
    }else{
        
        self.starView.currentRating = merchants_goods_comments_star_level;
    }
    
    int merchants_goods_comments_number = [model.merchants_goods_comments_number intValue];
    self.commitLabel.text = [NSString stringWithFormat:@"%d评论",merchants_goods_comments_number];
    
    int merchants_goods_type = [model.merchants_goods_type intValue];
    if (merchants_goods_type == 1) {

        self.dateLabel.text = [NSString stringWithFormat:@"%@",model.merchants_goods_available_time];
    }else{
//
//        NSDictionary * dict = [self dictionaryWithJsonString:model.merchants_goods_available_time];
//        NSArray *keysArray = [dict allKeys];
//        NSString * key = keysArray.firstObject;
//        NSArray * value = dict[key];
//        NSString *string = [value componentsJoinedByString:@","];
//        NSString * weekday;
//        int week = [key intValue];
//        switch (week) {
//            case 1:
//                weekday = @"周一";
//                break;
//            case 2:
//                weekday = @"周二";
//                break;
//            case 3:
//                weekday = @"周三";
//                break;
//            case 4:
//                weekday = @"周四";
//                break;
//            case 5:
//                weekday = @"周五";
//                break;
//            case 6:
//                weekday = @"周六";
//                break;
//            case 7:
//                weekday = @"周日";
//                break;
//
//            default:
//                break;
//        }
    self.dateLabel.text = [NSString stringWithFormat:@"场次:%@",model.goods_order_goods_field];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF5F5F5);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    self.orderNumLabel = [self setupOrderLabelWithTitle:@"订单编号：4158393784932"];
    [self.backView addSubview:self.orderNumLabel];
    self.orderDateLabel = [self setupOrderLabelWithTitle:@"订单日期:2018-09-03"];
    [self.backView addSubview:self.orderDateLabel];
    self.totalLabel = [self setupOrderLabelWithTitle:@"合计"];
    [self.backView addSubview:self.totalLabel];
    
    self.oneLineView = [self setupLineView];
    [self.backView addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self.backView addSubview:self.twoLineView];
    
    UILabel * stateLabel = [UILabel new];
    
    stateLabel.textColor = ColorWithRGB(0x52d498);
    stateLabel.text = @"待付款";
    stateLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.stateLabel = stateLabel;
    
    [self.backView addSubview:self.stateLabel];
    
    
    UIImageView * goodsImageView = [UIImageView new];
    
    goodsImageView.backgroundColor = [UIColor lightGrayColor];
    
    self.goodsImageView = goodsImageView;
    
    [self.backView addSubview:self.goodsImageView];
    
    
    UILabel * goodsLabel = [UILabel new];
    
    goodsLabel.textColor = ZRBlackColor;
    goodsLabel.text = @"商品名称";
    goodsLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    
    self.goodsLabel = goodsLabel;
    
    [self.backView addSubview:self.goodsLabel];
    
    
    UILabel * countLabel = [UILabel new];
    
    countLabel.textColor = [UIColor lightGrayColor];
    countLabel.text = @"*1";
    countLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    countLabel.textAlignment = NSTextAlignmentRight;
    
    self.countLabel = countLabel;
    
    [self.backView addSubview:self.countLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(107 * SCREENH6, 80 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self.backView addSubview:self.starView];
    
    
    UILabel * commitLabel = [UILabel new];
    
    commitLabel.textColor = [UIColor lightGrayColor];
    commitLabel.text = @"11评论";
    commitLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.commitLabel = commitLabel;
    
    [self.backView addSubview:self.commitLabel];
    
    
    UILabel * dateLabel = [UILabel new];
    
    dateLabel.textColor = ZRBlackColor;
    dateLabel.text = @"9月11日 - 9月21日";
    dateLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.dateLabel = dateLabel;
    
    [self.backView addSubview:self.dateLabel];
    
    
    UIView * smallView = [UIView new];
    
    smallView.backgroundColor = ColorWithRGB(0xFFE055);
    
    self.smallView = smallView;
    
    [self.backView addSubview:self.smallView];
    
    UILabel * priceLabel = [UILabel new];
    
    priceLabel.textColor = ZRBlackColor;
    priceLabel.text = @"$21.4";
    priceLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.priceLabel = priceLabel;
    
    [self.backView addSubview:self.priceLabel];
    
    UILabel * orignLabel = [UILabel new];
    
    orignLabel.textColor = ZRBlackColor;
    orignLabel.text = @"原价$40.2";
    orignLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.orignLabel = orignLabel;
    
    [self.backView addSubview:self.orignLabel];
    
    
    UIImageView * discountsImageView = [UIImageView new];
    
    discountsImageView.image = [UIImage imageNamed:@"coupon(1)"];
    
    self.discountsImageView = discountsImageView;
    
    [self.backView addSubview:self.discountsImageView];
    
    
    UILabel * discountsLabel = [UILabel new];
    
    discountsLabel.textColor = ZRBlackColor;
    discountsLabel.text = @"优惠券";
    discountsLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.discountsLabel = discountsLabel;
    
    [self.backView addSubview:self.discountsLabel];
    
    
    UILabel * moneyLabel = [UILabel new];
    
    moneyLabel.textColor = ZRBlackColor;
    moneyLabel.text = @"--";
    moneyLabel.font = [UIFont boldSystemFontOfSize:14 * SCREENH6];
    
    self.moneyLabel = moneyLabel;
    
    [self.backView addSubview:self.moneyLabel];
    
    
    UIButton * cancleButton = [UIButton new];
    
    [cancleButton setTitle:@"取消订单" forState:UIControlStateNormal];
    [cancleButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [cancleButton setBackgroundColor:ZRWhiteColor];
    cancleButton.layer.cornerRadius = 5;
    cancleButton.layer.masksToBounds = YES;
    cancleButton.layer.borderColor = [ZRBlackColor CGColor];
    cancleButton.layer.borderWidth = 1 * SCREENH6;
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [cancleButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.cancleButton = cancleButton;
    
    [self.backView addSubview:self.cancleButton];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"立即付款" forState:UIControlStateNormal];
    [sureButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [sureButton setBackgroundColor:ColorWithRGB(0xFFE055)];
    sureButton.layer.cornerRadius = 5;
    sureButton.layer.masksToBounds = YES;
    sureButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    [sureButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.sureButton = sureButton;
    
    [self.backView addSubview:self.sureButton];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(220 * SCREENH6);
    }];
    
    [self.orderNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(15 * SCREENH6);
    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.orderNumLabel);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.orderNumLabel);
        make.trailing.mas_equalTo(self.stateLabel);
        make.top.mas_equalTo(self.orderNumLabel.mas_bottom).offset(15 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];

    [self.goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.width.mas_equalTo(90 * SCREENH6);
        make.height.mas_equalTo(95 * SCREENH6);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.goodsImageView);
        make.trailing.mas_equalTo(self.backView).offset(-15 * SCREENH6);
    }];
    
    [self.goodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsImageView.mas_trailing).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.countLabel.mas_leading).offset(-5 * SCREENH6);
        make.centerY.mas_equalTo(self.countLabel);
    }];

    [self.commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.starView);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.goodsLabel);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(10 * SCREENH6);
        make.width.mas_equalTo(30 * SCREENH6);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.dateLabel);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.orignLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.priceLabel.mas_trailing).offset(5 * SCREENH6);
        make.top.mas_equalTo(self.dateLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.discountsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.priceLabel);
        make.top.mas_equalTo(self.smallView.mas_bottom).offset(3 * SCREENH6);
        make.width.mas_equalTo(25 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.discountsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.discountsImageView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.discountsImageView);
    }];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.countLabel);
        make.centerY.mas_equalTo(self.discountsLabel);
    }];
    
    [self.totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.moneyLabel);
        make.trailing.mas_equalTo(self.moneyLabel.mas_leading).offset(-2 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.goodsImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.orderDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.orderDateLabel);
        make.trailing.mas_equalTo(self.twoLineView);
        make.width.mas_equalTo(70 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.sureButton);
        make.trailing.mas_equalTo(self.sureButton.mas_leading).offset(-10 * SCREENH6);
    }];
}


- (UILabel *)setupOrderLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:11 * SCREENH6];
    label.text = title;
    
    return label;
}


- (UIView *)setupLineView{

    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF5F5F5);
    
    return lineView;
}


- (void)clickButton:(UIButton *)button{
    
//    if (button == self.cancleButton) {
//
//        if (self.myOrderListTableViewCellDelegate && [self.myOrderListTableViewCellDelegate respondsToSelector:@selector(clickItemButton: WithCell:)])
//        {
//            [self.myOrderListTableViewCellDelegate clickItemButton:button.titleLabel.text WithCell:self];
//        }
//    }else{
//        
////        if (self.clickSureButtonBlock) {
////
////            self.clickSureButtonBlock(self);
////        }
//        
//    }
    
    if (self.myOrderListTableViewCellDelegate && [self.myOrderListTableViewCellDelegate respondsToSelector:@selector(clickItemButton: WithCell:)])
    {
        [self.myOrderListTableViewCellDelegate clickItemButton:button.titleLabel.text WithCell:self];
    }
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString{
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
